package components;

import common.Constant;
import operator.Handler;
import platform.AttrOption;
import pri.swg.Dragger;
import pri.swg.FadeFrame;
import pri.swg.Fader;
import common.Singleton;
import view.Main;
import view.ViewControl;
import vo.Attr;
import vo.Mean;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

/**
 * 弹出询问框
 * @author 柴家琪
 */
public class MDialog extends FadeFrame{
    private static final long serialVersionUID = 1L;
    public static final int DELETE=-1, EXIT=-2, REMOVE_MEAN = -3, ADD=1, ADD_FOLDER=2;

    private Main main;
    private ViewControl vc;
    private Handler handler;

    private MLabel head;
    private String sourceText="";
    private Font font=new Font(Constant.roundfaceName, Font.BOLD, 18);

    private JTextField input;

    private AttrOption attrOption;
    private ArrayList<Mean> meansNeedToDelete;

    public MDialog(String title, AttrOption attrOption, int type){
        this(title, type);
        this.attrOption = attrOption;
    }

    // Dialog for delete means
    public MDialog(String title, ArrayList<Mean> meansNeedToDelete) {
        this(title, REMOVE_MEAN);
        this.meansNeedToDelete = meansNeedToDelete;
    }


   public MDialog(String title, int type) {
        this.vc = Singleton.vc();
        this.handler = Singleton.handler();
        this.main = Main.main;

        set(0.2f,1.0f,100,true,true);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-300)/2, (screenSize.height-168)/2,300,168);
        setBackground(Color.black);
        setUndecorated(true);
        setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        vc.hide();

       JPanel content = new JPanel();
        Color borderColor = Color.LIGHT_GRAY;
        if(type<0){
            head =new MLabel(title, borderColor = Color.RED);
            head.setPreferredSize(new Dimension(300, 100));
            head.setIcon(Singleton.resource().getIcon("red_help.png"));
            content.setPreferredSize(new Dimension(300, 68));
        }else if(type==ADD){
            sourceText="添加候选词<br/>类别："+title;
            head =new MLabel(alertString(sourceText, ""), borderColor = Color.cyan);
            head.setPreferredSize(new Dimension(300, 70));
            head.setIcon(Singleton.resource().getIcon("cyan_info.png"));
            content.setPreferredSize(new Dimension(300, 48));
        }else if(type==ADD_FOLDER){
            sourceText="增加目录<br/>位置："+title+"->";
            head =new MLabel(alertString(sourceText, ""), borderColor = Color.yellow);
            head.setPreferredSize(new Dimension(300, 70));
            head.setIcon(Singleton.resource().getIcon("yellow_info.png"));
            content.setPreferredSize(new Dimension(300, 48));
        }
//		content.setBorder(new MatteBorder(0, 0, 1, 0, borderColor));
        head.setBorder(new MatteBorder(2, 0, 0, 0, borderColor));
        add(head);

        content.setBackground(new Color(120,120,120));
        content.setLayout(new GridLayout(1, 2));
        MLabel confirm,cancle,save,unsave;

        switch (type) {
            case DELETE:
                confirm=new MLabel(1);
                cancle=new MLabel(2);
                confirm.addMouseListener(new MouseAdapter() {
                    public void mouseReleased(MouseEvent e){
                        handler.delete();
                        fadeOut();
                        vc.show();
                    }
                });
                cancle.addMouseListener(new MouseAdapter() {
                    public void mouseReleased(MouseEvent e){
                        handler.cancled();
                        fadeOut();
                        vc.show();
                    }
                });
                content.add(confirm);
                content.add(cancle);
                break;
            case EXIT:
                cancle=new MLabel(2);
                save=new MLabel(3);
                unsave=new MLabel(4);
                cancle.addMouseListener(new MouseAdapter(){
                    public void mouseReleased(MouseEvent e){
                        fadeOut();
                        vc.show();
                    }
                });
                save.addMouseListener(new MouseAdapter() {
                    public void mouseReleased(MouseEvent e){
                        if (handler.editModel) {
                            main.setSpliterDivideLoc(main.recordDivideLoc);
                        }
                        main.exit(true);
                    }
                });
                unsave.addMouseListener(new MouseAdapter(){
                    public void mouseReleased(MouseEvent e){
                        main.exit(false);
                    }
                });
                content.add(unsave);
                content.add(cancle);
                content.add(save);
                break;
//            case REMOVE_MEAN:
//                confirm = new MLabel(1);
//                cancle = new MLabel(2);
//                confirm.addMouseListener(new MouseAdapter() {
//                    public void mouseReleased(MouseEvent e) {
//                        main.outModifyMode(false);
//                        fadeOut();
//                    }
//                });
//                cancle.addMouseListener(new MouseAdapter() {
//                    public void mouseReleased(MouseEvent e) {
//                        main.outModifyMode(true);
//                        fadeOut();
//                    }
//                });
//                break;
            case ADD:
                setInputer(Color.cyan);
                add(input);
                confirm=new MLabel(1);
                cancle=new MLabel(2);

                // 确定按钮监听
                confirm.addMouseListener(new MouseAdapter() {
                    public void mouseReleased(MouseEvent e){
                        String check= checkInputText();
                        if(check!=null){
                            head.setText(alertString(sourceText, check));
                        }else{
                            if(attrOption.existCandidate(input.getText())){
                                input.setText("");
                                head.setText(alertString(sourceText, "字段已经存在！请重新输入"));
                            }else{
                                attrOption.getAttr().addNew(input.getText(),true);//添加
                                attrOption.showList();
                                fadeOut();
                                vc.show();
                            }
                        }
                    }
                });

                // 取消按钮监听
                cancle.addMouseListener(new MouseAdapter() {
                    public void mouseReleased(MouseEvent e){
                        fadeOut();
                        vc.show();
                    }
                });
                content.add(confirm);
                content.add(cancle);
                break;
            case ADD_FOLDER:
                setInputer(Color.YELLOW);
                add(input);
                confirm=new MLabel(1);
                cancle=new MLabel(2);
                confirm.addMouseListener(new MouseAdapter() {
                    public void mouseReleased(MouseEvent e){
                        String check= checkInputText();
                        if(check!=null){
                            head.setText(alertString(sourceText, check));
                        }else{
                            if(attrOption.existFolder(input.getText())){
                                input.setText("");
                                head.setText(alertString(sourceText, "目录已经存在！请重新输入"));
                            }else{
                                attrOption.getAttr().addFolder(new Attr(input.getText(), attrOption.getAttr().getIndex()), true);
                                attrOption.showList();
                                fadeOut();
                                vc.show();
                            }
                        }
                    }
                });
                cancle.addMouseListener(new MouseAdapter() {
                    public void mouseReleased(MouseEvent e){
                        fadeOut();
                        vc.show();
                    }
                });
                content.add(confirm);
                content.add(cancle);
                break;
        }
        add(content);
        Dragger.drag(this, head);
        setVisible(true);
    }
    private void setInputer(Color cc){
        input=new JTextField();
        input.setHorizontalAlignment(JTextField.CENTER);
        input.setFont(font);
        input.setBorder(null);
        input.setPreferredSize(new Dimension(300, 50));
        input.setBackground(Color.DARK_GRAY);
        input.setForeground(Color.WHITE);
        input.setCaretColor(cc);
    }
    private String checkInputText(){
        String s=input.getText();
        boolean invalid=true,illegal=false;//无效、非法
        for(int i=0;i<s.length();i++){
            invalid=false;
            switch(s.charAt(i)){
                case ' ':
                case 13:
                case 10:invalid=true;
                    break;
                case '&':
                case '$':
                case '#':
                case '~':
                case '【':
                case '】':
                case '：':
                case '；':
                case '、':
                case '/':illegal=true;
                    break;
            }
        }
        if(invalid)
            return "请输入有效字段！";
        else if(illegal)
            return "&$#~【】：；、/等符号非法！";
        return null;
    }

    private String alertString(String s1, String s2) {
        return "<html><p align='center'>" + s1 + "</p>" + s2 + "</html>";
    }
}
class MLabel extends JLabel{
    private static final long serialVersionUID = 1L;
    /**
     * 按钮
     */
    MLabel(int type){
        this();
        setBackground(Color.BLACK);
        setForeground(Color.WHITE);

        String text = "", icon = "";
        switch(type){
            case 1:
                text = "确定";
                icon = "yes";
                break;
            case 2:
                text = "取消";
                icon = "no";
                break;
            case 3:
                text = "保存";
                icon = "save_now";
                break;
            case 4:
                text = "不保存";
                icon = "not_save";
        }
        setText(text);
        setIcon(Singleton.resource().getIcon(icon + ".png"));
        Fader.fade(this, Fader.BACK_GROUND, 200, Fader.LISTEN_MouseEnter, new Color(200, 205, 255));
        Fader.fade(this, Fader.FORE_GROUND, 300, Fader.LISTEN_MouseEnter, Color.BLACK);

        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e){
                setBorder(new LineBorder(Color.white, 2));
            }
            public void mouseReleased(MouseEvent e){
                setBorder(null);
            }
        });
    }
    /**
     * 内容
     */
    MLabel(String title, Color fg){
        this();
        setBackground(Color.black);
        setForeground(fg);
        setText(title);
    }
    private MLabel(){
        setOpaque(true);
        setHorizontalAlignment(JLabel.CENTER);
        setFont(new Font(Constant.boldfaceName, Font.BOLD,16));
    }
}
